import { Control } from 'react-keeper';
import { Toast } from 'saltui';
const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        schoolId: '',
        active: false
    },

    onSubmit: function (params, cb) {
        let t = this;
        let data = { "evaluateStudentList": params.selectStudentList };
        DB.Teacher.evaluate_edit_save({
            evaluateId: params.evaluateId,//评价id
            data: data,
            evaluateTitleId: params.evaluateTitleId,
            evaluateTitle: params.evaluateTitle,
            evaluateRankId: params.evaluateRankId.id,
            remark: params.remark,
            schoolId: Storage.get('user.schoolId') || "f20171120",
            teacherId: Storage.get('user.teacherId') || 4732,
            teacherName: Storage.get('user.talkId') || '钉钉2号老师'
        })
            .then((content) => {
                if (content.code == 1) {
                    if (params.formData && params.formData.getAll("files").length > 0) {
                        return { schoolId: content.check.schoolId, evaluateId: content.data.evaluateId, formData: params.formData }
                    } else {
                        Toast.show({
                            type: 'success',
                            content: '添加评价成功',
                            hasMask: true,
                            duration: 2000,
                        });
                        this.data={}//清空数据
                        Control.go(-1)
                    }
                }
            })
            .then((params) => {
                if (!params) return;
                let url = "/picture/uploadPictureEvaluate?schoolId=" + params.schoolId + "&evaluateId=" + params.evaluateId;
                fetch(url, {
                    method: "POST",
                    body: params.formData
                }).then(function (response) {
                    return response.json();
                }).then(function (result) {
                    if (result.content.code == 1) {
                        Toast.show({
                            type: 'success',
                            content: '添加评价成功',
                            hasMask: true,
                            duration: 2000,
                        });
                        t.data={}//清空数据
                        Control.go(-1);
                    }
                }, function (error) {

                })
            })
            .catch(function (error) {

            });
    },
    onGetClassList: function (params, cb) {
        let t = this;
        var selectType = params.selectType;
        if (t.data.data_studentList && t.data.classList && t.data.data_studentList.hasOwnProperty(t.data.classList[0].classId)) {
            t.data.studentList = t.data.data_studentList[t.data.classId];
            t.data.selectType = params.selectType;
            t.data.usertitle = '选择评价学生'
            t.data.active = true;
            t.updateComponent();
            return;
        }
        DB.Teacher.get_class_lists({
            schoolId: Storage.get('user.schoolId') || "f20171120",
            teacherId: Storage.get('user.teacherId') || 4732
        })
            .then((content) => {
                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    this.data.schoolId = schoolId;
                    let teacherId = content.check.teacherId;
                    content = content.data.list;
                    var list = [];
                    var obj = {};
                    if (content && content.length > 0) {
                        for (let item of content) {
                            obj.imgUrl = '';
                            obj.classId = item.id;
                            obj.title = item.name;
                            list.push(obj);
                            obj = {};
                        }
                        t.data.classList = list;
                        t.data.classId = content[0].id;
                    }
                    return { schoolId, classId: content[0].id };
                }
            }).then((params) => {
                if (params) {
                    DB.Teacher.get_student_list({
                        schoolId: params.schoolId,
                        classId: params.classId
                    })
                        .then((content) => {
                            if (content.code == 1) {
                                let schoolId = content.check.schoolId;
                                let classId = content.check.classId;
                                content = content.data.studentList;
                                if (content && content.length > 0) {
                                    t.data.studentList = content;
                                    t.data.selectType = selectType;
                                    t.data.active = true;
                                    t.data.usertitle = '选择评价学生'
                                    //存储数据
                                    t.data.data_studentList = {
                                        [classId]: content
                                    }
                                }
                                cb && cb()
                                t.updateComponent();
                            }
                        })
                }
            })
            .catch(function (error) {

            });
    },
    onGetStudentList: function (params, cb) {
        let t = this;
        if (t.data.data_studentList && t.data.data_studentList.hasOwnProperty(params.classId)) {
            t.data.studentList = t.data.data_studentList[params.classId];
            t.data.classId = params.classId;
            t.updateComponent();
            return;
        }
        DB.Teacher.get_student_list({
            schoolId: this.data.schoolId,
            classId: params.classId
        })
            .then((content) => {
                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    let classId = content.check.classId;
                    content = content.data.studentList;
                    if (content && content.length > 0) {
                        t.data.studentList = content;
                        //存储数据
                        t.data.data_studentList[classId] = content;
                        t.data.classId = params.classId;
                    }
                    t.updateComponent();
                }
            })
            .catch(function (error) {

            });
    },
    onGetEvaluateTitle: function (params, cb) {
        let t = this;
        let schoolId = Storage.get('user.schoolId');
        if (t.data.data_evaluateList && t.data.data_evaluateList.hasOwnProperty(schoolId)) {
            t.data.evaluateList = t.data.data_evaluateList[schoolId];
            t.data.selectType = params.selectType;
            t.data.usertitle = '选择违纪类别'
            t.data.active = true;
            if (params.selectStudentList && params.selectStudentList.length > 0) {
                t.data.selectStudentList = params.selectStudentList;
            }
            t.updateComponent();
            return;
        }
        DB.Teacher.get_evaluate_title({
            schoolId: Storage.get('user.schoolId') || "f20171120"
        })
            .then((content) => {
                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    if (params.selectStudentList && params.selectStudentList.length > 0) {
                        t.data.selectStudentList = params.selectStudentList;
                    }
                    content = content.data.list;
                    var list = [];
                    var obj = {};
                    if (content && content.length > 0) {
                        for (let item of content) {
                            for (let subitem of item.childList) {
                                obj.imgUrl = '';
                                obj.themeChildId = subitem.id;
                                obj.title = subitem.name;
                                list.push(obj);
                                obj = {};
                            }
                            item.childList = list;
                            list = [];
                        }
                        t.data.evaluateList = content;
                        t.data.selectType = params.selectType;
                        t.data.usertitle = '选择违纪类别'
                        t.data.active = true;
                        //存储数据
                        t.data.data_evaluateList = {
                            [schoolId]: content
                        }
                    }
                    cb && cb(true)
                    t.updateComponent();

                }
            })
            .catch(function (error) {

            });
    },
    onGetEvaluateRank: function (params, cb) {
        let t = this;
        DB.Teacher.get_evaluate_rank({
            schoolId: Storage.get('user.schoolId') || "f20171120"
        })
            .then((content) => {
                if (content.code == 1) {
                    if (this.data.active) {
                        this.data.active = false;
                    }
                    let schoolId = content.check.schoolId;
                    content = content.data.list;
                    let list = [];
                    if (content && content.length > 0) {
                        for (let item of content) {
                            list.push(item.msg);
                        }
                        t.data.evaluateRankList = content;
                        t.data.tabContent = list;
                    }
                    t.updateComponent();

                }
            })
            .catch(function (error) {

            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
